package com.mjh.dao;

import java.util.ArrayList;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import com.mjh.bean.Task;
import com.mjh.util.ResourceUtil;
import com.mjh.util.XMLParser;

public class TaskDAO {

	public static Logger log = Logger.getLogger(TaskDAO.class);

	public Task getTask(Element el){
		Task task = new Task();
		task.setId(el.attributeValue("id"));
		task.setName(el.attributeValue("name"));
		task.setClassPath(el.attributeValue("class"));
		task.setSchedule(el.attributeValue("schedule"));
		return task;
	}

	public ArrayList<Task> getTaskList(){
		ArrayList<Task> tasks = new ArrayList<Task>();
		//由于需要动态加载任务，所以必须要每次都重新读取配置文件
		XMLParser taskXML = new XMLParser(ResourceUtil.getRealPath("com/mjh/config/task.xml"));
		Iterator<Element> it = taskXML.listAllNodeFromRoot();
		while(it.hasNext()){
			tasks.add(getTask((Element)it.next()));
		}
		return tasks;
	}
}
